<?php
class Pagination extends CWidget {
    public $pagination;
    public $baseUrl;
  
    public function run() {
        $cs=Yii::app()->clientScript;
        $cs->registerCssFile(Yii::app()->request->baseUrl."/css/front/paging.css");
        
        // pages info
        $totalPage = $this->pagination->getPageCount();
        $page       = $this->pagination->currentPage+1;
        $limitPage	= 5;
        $fromPage	= intval(($page-1)/$limitPage)*$limitPage+1;
        $toPage		= $fromPage+$limitPage-1;
        $toPage		= ($toPage > $totalPage)? $totalPage:$toPage;

        // items info
        $totalItem = $this->pagination->getItemCount();
        $pageSize  = $this->pagination->getPageSize();
        $fromItem  = ($page-1)*$pageSize+1;
        $toItem    = $page*$pageSize;
        $toItem    = ($toItem > $totalItem)?$totalItem:$toItem;

        // <!--start paging div-->
        echo CHtml::openTag("div", array('class'=>'pagings'));

        // <!--display items info-->
        echo CHtml::openTag("div", array('class'=>'column1'));
        echo Yii::t('FrontEnd', "Display from {from}-{to} of {total}", array('{from}'=>$fromItem, '{to}'=>$toItem, '{total}'=>$totalItem));
        echo CHtml::closeTag("div");
        // <!--display paging-->
        if($totalPage > 1)
        {
            echo CHtml::openTag("ul");
            if($fromPage > 1)
            {
                echo CHtml::openTag("li");
                echo CHtml::link(Yii::t('FrontEnd', "Previous"), $this->createPageUrl($page-1), array('class'=>'paging-item'));
                echo CHtml::closeTag("li");
            }
            for($i=$fromPage; $i<=$toPage; $i++)
            {
                if($i==$page)
                {
                    echo CHtml::openTag("li", array('class'=>'active'));
                    echo "<b>$i</b>";
                    echo CHtml::closeTag("li");
                }
                else
                {
                    echo CHtml::openTag("li");
                    echo CHtml::link($i, $this->createPageUrl($i), array('class'=>'paging-item'));
                    echo CHtml::closeTag("li");
                }
            }
            if($toPage < $totalPage)
            {
                echo CHtml::openTag("li");
                echo CHtml::link(Yii::t('FrontEnd', "Next"), $this->createPageUrl($toPage+1), array('class'=>'paging-item'));
                echo CHtml::closeTag("li");
            }

            echo CHtml::closeTag("ul");
        }
        echo CHtml::closeTag("div");
        //<!-- end paging -->
    }

    function createPageUrl($page=0) {
        $params = $_GET;
        unset($params[$this->pagination->pageVar]);
		if($page>0) $params["page"]=$page;
        return $this->controller->createFriendlyUrl($params);
    }
}
?>